package cn.demoa.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class AddStuActivity extends AppCompatActivity {
    private EditText editAddName;
    private EditText editAddAge;
    private RadioButton radioBtn1;
    private RadioButton radioBtn2;
    private RadioButton radioBtn3;
    private RadioButton radioBtn4;
    private RadioButton radioBtn5;
    private Button btnAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_stu);
        setTitle("添加学生");

        findView();

        setBtnAddClickListener();

    }

    /*设置添加按钮的点击事件*/
    private void setBtnAddClickListener() {
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = editAddName.getText().toString();
                int age = Integer.parseInt(editAddAge.getText().toString());

                int imgId = R.drawable.dog1;
                if (radioBtn1.isChecked()) {
                    imgId = R.drawable.dog1;
                } else if (radioBtn2.isChecked()) {
                    imgId = R.drawable.dog2;
                } else if (radioBtn3.isChecked()) {
                    imgId = R.drawable.dog3;
                } else if (radioBtn4.isChecked()) {
                    imgId = R.drawable.dog4;
                } else if (radioBtn5.isChecked()) {
                    imgId = R.drawable.dog5;
                }

                Intent intent = new Intent();
                intent.putExtra("NAME",name);
                intent.putExtra("AGE",age);
                intent.putExtra("IMG",imgId);
                setResult(2001,intent);

                finish();
            }
        });
    }

    /*绑定各个组件*/
    private void findView() {
        editAddName = findViewById(R.id.edit_addName);
        editAddAge = findViewById(R.id.edit_addAge);

        radioBtn1 = findViewById(R.id.add_radioBtn1);
        radioBtn2 = findViewById(R.id.add_radioBtn2);
        radioBtn3 = findViewById(R.id.add_radioBtn3);
        radioBtn4 = findViewById(R.id.add_radioBtn4);
        radioBtn5 = findViewById(R.id.add_radioBtn5);

        btnAdd = findViewById(R.id.btn_add);
    }
}
